﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using docmgr.DAL;

namespace docmgr.BLL {
    public class Department {

        //--表格名称
        const string m_TableName = "Department";

        docmgr.DAL.Department dal = new docmgr.DAL.Department();

        public Department() {
            dal.InitiConnect2Adapter();
        }

        #region  成员方法组1：对表进行查询,添加,修改,删除操作

        #region 对表进行查询 (有6个方法)
        /// <summary>
        /// 根据主键值获取一条记录
        /// </summary>
        /// <param name="key">主键值</param>
        /// <returns></returns>
        public System.Data.DataTable GetModel(string key)
        {
            if (string.IsNullOrEmpty(key))
                key = "0";
            return dal.GetTable(string.Format("Select Top 1 * from [Department] Where ID={0}", key));
        }
        /// <summary>
        /// 根据主键值获取一条记录
        /// </summary>
        /// <param name="fields">SQL查询字段</param>
        /// <param name="key">主键值</param>
        /// <returns></returns>
        public System.Data.DataTable GetModel(string fields, string key)
        {
            if (string.IsNullOrEmpty(fields))
                fields = "*";
            if (string.IsNullOrEmpty(key))
                key = "0";
            return dal.GetTable(string.Format("Select Top 1 {0} from [Department] Where ID={1}", fields, key));
        }
        /// <summary>
        /// 根据自定义条件获取一条或多条记录(查询所有字段)
        /// </summary>
        /// <param name="condition">SQL查询条件</param>
        /// <returns></returns>
        public System.Data.DataTable GetList(string condition)
        {
            if (string.IsNullOrEmpty(condition))
                condition = "0=0";
            return dal.GetTable(string.Format("Select * from [Department] Where {0}", condition));
        }
        /// <summary>
        /// 根据自定义条件获取一条或多条记录(可以自定义查询字段)
        /// </summary>
        /// <param name="fields">SQL查询字段</param>
        /// <param name="condition">SQL查询条件</param>
        /// <returns></returns>
        public System.Data.DataTable GetList(string fields, string condition)
        {
            if (string.IsNullOrEmpty(fields))
                fields = "*";
            if (string.IsNullOrEmpty(condition))
                condition = "0=0";
            return dal.GetTable(string.Format("Select {0} from [Department] Where {1}", fields, condition));
        }
        /// <summary>
        /// 根据SQL命令获取一个表的分页记录
        /// (获取一个表的多条记录)
        /// </summary>
        /// <param name="fields">SQL查询字段</param>
        /// <param name="condition">SQL查询条件</param>
        /// <param name="orderBy">排序方式</param>
        /// <param name="PageSize">分页大小</param>
        /// <param name="PageIndex">当前页码</param>        
        /// <returns></returns>
        public System.Data.DataTable GetPage(string fields, string condition, string orderBy, int PageSize, int PageIndex)
        {
            string sql = string.Format("Select {0} from [Department] Where {1} {2}", fields, condition, orderBy);
            return dal.GetTable(sql, PageSize, PageIndex);
        }
        /// <summary>
        /// 根据一组SQL命令获取一个DataSet
        /// (可以获取多个表的多条记录)
        /// </summary>
        /// <param name="sqlList">一组SQL命令</param>
        /// <returns></returns>
        public System.Data.DataSet GetDataSet(IList<string> sqlList)
        {
            return dal.GetDataSet(sqlList);
        }
        #endregion

        #region  对表进行添加,修改 (有2个方法)
        /// <summary>
        /// 更新一个表格(可以更新一条或多条记录)
        /// </summary>
        /// <param name="table">有一条或多条记录的DataTable</param>
        /// <returns></returns>
        public int Update(System.Data.DataTable table) {
            return dal.Update(table);
        }
        /// <summary>
        /// 使用事务
        /// 更新一个表格(可以更新一条或多条记录)
        /// </summary>
        /// <param name="table">有一条或多条记录的DataTable</param>
        /// <returns></returns>
        public int UpdateWithTrans(System.Data.DataTable table)
        {
            return dal.UpdateWithTrans(table);
        }
        #endregion

        #region 对表进行删除 (有5个方法)
        /// <summary>
        /// 通过一个主键值删除一条记录
        /// </summary>
        /// <param name="key">主键值</param>
        /// <returns>返回删除的行数</returns>
        public int DeleteByKey(string key)
        {
            string sql = "DELETE FROM [Department] Where ID = {0}";
            if (!string.IsNullOrEmpty(key))
            {
                return dal.Delete(string.Format(sql, key));
            }
            return 0;
        }

        /// <summary>
        /// 通过自定义条件删除一条或多条记录
        /// </summary>
        /// <param name="condition">自定义条件,为NULL或空就删除所有记录</param>
        /// <returns>返回删除的行数</returns>
        public int Delete(string condition)
        {
            string sql = "DELETE FROM [Department] ";
            if (!string.IsNullOrEmpty(condition))
            {
                sql += " WHERE " + condition;
            }
            return dal.Delete(sql);
        }
       
        /// <summary>
        /// 使用事务
        /// 通过自定义条件删除一条或多条记录
        /// </summary>
        /// <param name="condition">自定义条件,为NULL或空就删除所有记录</param>
        /// <returns>返回删除的行数</returns>
        public int DeleteWithTrans(string condition)
        {
            string sql = "DELETE FROM [Department] ";
            if (!string.IsNullOrEmpty(condition))
            {
                sql += " WHERE " + condition;
            }
            return dal.DeleteWithTrans(sql);
        }
               
        /// <summary>
        /// 通过一组主键删除多条记录
        /// </summary>
        /// <param name="keys"></param>
        /// <returns>返回删除的行数</returns>
        public int DeleteByKey(System.Collections.Generic.List<string> keys)
        {
            return dal.DeleteByKey(keys);
        }
       
        /// <summary>
        /// 使用事务
        /// 通过一组主键值删除多条记录
        /// </summary>
        /// <param name="keys">一组主键值</param>
        /// <returns>返回删除的行数</returns>
        public int DeleteByKeyWithTrans(System.Collections.Generic.List<string> keys)
        {
            return dal.DeleteByKeyWithTrans(keys);
        }
        #endregion

        #endregion

        #region 成员方法组2： 主副表操作
        // 以下是主副表操作, [4/8/2005 Administrator]
        // 获取一个主副表     GetMain2DetailsDataSet
        // 使用事务更新主副表 UpdateMain2DetailsWithTrans
        // 使用事务删除主副表 DeleteMain2DetailsByKeyWithTrans
        // 如果没有主副表可以 不使用这三个方法

        /// <summary>
        /// 根据一组SQL命令获取一个主副表DataSet
        /// </summary>
        /// <param name="sqlList">一组SQL命令,第一个sql命令默认获取主表记录</param>
        /// <returns></returns>
        public System.Data.DataSet GetMain2DetailsDataSet(IList<string> sqlList)
        {
            return dal.GetMain2DetailsDataSet(sqlList);
        }

        /// <summary>
        /// 使用事务更新主副表DataSet
        /// </summary>
        /// <param name="ds"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool UpdateMain2DetailsWithTrans(System.Data.DataSet ds, out string msg)
        {
            return dal.UpdateMain2DetailsWithTrans(ds, out msg);
        }
        /// <summary>
        /// 使用事务
        /// 通过主表主键值删除主副表
        /// </summary>
        /// <param name="key">主表主键</param>
        /// <param name="msg">返回消息</param>
        /// <returns></returns>
        public bool DeleteMain2DetailsByKeyWithTrans(string key, out string msg)
        {
            return dal.DeleteMain2DetailsByKeyWithTrans(key, out msg);
        }

        #endregion
           

        #region 成员方法组3：你增加的新方法
        //
        //
        //
        //
        #endregion
    }
}
